﻿using PLCS.Domain.Menus;

namespace PLCS.Domain.Roles;

public class Role : BaseEntity
{
    /// <summary>
    /// 权限名称
    /// </summary>
    [Description("权限名称")]
    public string RoleName { get; set; }

    /// <summary>
    /// 权限说明
    /// </summary>
    [Description("权限说明")]
    public string Description { get; set; }

    /// <summary>
    /// 菜单集合（逻辑外键，用，号隔开）
    /// </summary>
    [Description("菜单集合")]
    public string? MenusString { get; set; }

    [NotMapped]
    public List<Menu>? Menus { get; set; }

    public Role()
    {
    }

    public Role(string roleName, string description, string menusString = null)
    {
        RoleName = roleName;
        Description = description;
        MenusString = menusString;
        CreationTime = DateTime.Now;
    }

    public void AddMenu(Menu menu)
    {
        if (string.IsNullOrEmpty(MenusString))
            MenusString += $"{menu.Title}";
        else
            MenusString += $"{ConstantChar.SplitChar}{menu.Title}";
    }

    public void AddMenus(List<Menu> menus)
    {
        foreach (var item in menus)
        {
            if (string.IsNullOrEmpty(MenusString))
                MenusString += $"{item.Title}";
            else
                MenusString += $"{ConstantChar.SplitChar}{item.Title}";
        }
    }

    public IEnumerable<string> Analysis()
    {
        List<string> analysis = new List<string>();
        if (string.IsNullOrEmpty(MenusString))
            return analysis;
        else
        {
            foreach (var item in MenusString.Split(ConstantChar.SplitChar))
            {
                analysis.Add(item);
            }
        }

        return analysis;
    }
}